package edu.byu.intex2.data.dao;

import edu.byu.intex2.data.bo.Customer;
import edu.byu.intex2.data.Cache;
import edu.byu.intex2.data.ConnectionPool;
import edu.byu.intex2.data.DataException;
import java.sql.*;

/**
 * Customer Data Access Object
 * @author Brady
 */
public class UsedCarDAO {

  /////////////////////////////////////////////
  ///   Singleton code

  private static UsedCarDAO instance = null;

  /** Creates a new instance of CustomerDAO */
  private UsedCarDAO() {
  }//constructor

  /** Retrieves the single instance of this class */
  public static synchronized UsedCarDAO getInstance() {
    if (instance == null) {
      instance = new UsedCarDAO();
    }
    return instance;
  }//getInstance

  ////////////////////////////////////////////
  ///   CREATE methods

  /** 
   * Creates a new customer in the database
   * set alreadyInDB to false
   * put customer in cache
   */
  public Customer create(String id) throws DataException {

  }//create


  ////////////////////////////////////////////
  ///   READ methods

  /** 
   * Reads an existing customer from the database
   * 1) Checks cache
   * 2) Gets a connection
   * 3) Calls read with connection argument
   */
  public Customer read(String id) throws DataException {
  }

  /** 
   * Internal method to read an existing customer from the database
   * 1) Check cache for object
   * 2) Create a prepared statement to execute sql query
   * 3) Create a resultset from the preparedstatement
   * 4) Return the customer
   */
  synchronized Customer read(String id, Connection conn) throws Exception {

  }//read


  /** 
   * Internal method to create a customer object from a record
   * 1) Check cache for object
   * 2) Instantiate new customer
   * 3) Set alreadyInDb to false
   * 4) Set all variables from result set
   * 5) Set dirty to true
   * 6) Return customer
   */
  synchronized Customer readRecord(ResultSet rs, Connection conn) throws Exception {
  }//readRecord

<<<<<<< .mine
    public List<Customer> getAll() throws DataException {
    //establish a connection from the pool, use prepared statement, use the search method below to run the statement, catch any errors
	//release the connection
  }
=======
  /////////////////////////////////////////////
  ///   UPDATE methods
  
  /** Saves an existing customer in the database */
  public void save(Customer customer) throws DataException {
    //get a connection from the pool, save all of the sales first using the other customer method and then commit them in the database
	//rollback if there were any errors saving, catch any other errors you might have
	//release the connection!!
	
  }
  
  /** Internal method to update a customer in the database */
  void save(Customer customer, Connection conn) throws Exception {
    //check cache first, if returns nothing, put the customer object to cache
	//decide whether to update or insert into the database
	
    }  
  
  /** Saves an existing customer to the database */
  private void update(Customer customer, Connection conn) throws Exception {
    
	//use a prepared statement to prevent sql injection
	//run the query and set dirty to fase
	//make sure to close the prepared statement
	
  }
  
  /** Inserts a new customer into the database */
  private void insert(Customer customer, Connection conn) throws Exception {
    //use a perpared statement
	//set the alreadyInDB variable to true
	//dirty to false
	//close the prepared statement
	
  }  
  
  
  
  
  
  /////////////////////////////////////////////////
  ///   DELETE methods
  
  /** We do not support deleting of business objects in this application */
  public void delete(Customer customer) throws DataException {
    throw new RuntimeException("Nice try. The delete function is not supported in this application.");
  }
>>>>>>> .r26

  /** Internal method to search by certain fields */
  private List<Customer> search(PreparedStatement stmt, Connection conn) throws Exception {
    //instantiate a list of Customers to add recordsets to. this will make going through the list easier
	//finally, close the statement and return the list
  }

  ////////////////////////////////////////////////
  ///   SEARCH methods

  /** Retrieves all customers in a list of arrays from the database */
  public List<String[]> getAll() throws DataException {
    //this method does not use the standard search method because we don't want to get EVERY customer object back from here, there could be 10's of
	//thousands in the future. instead, we want their full name and phone number so we can populate our search list. so our query should be designed 
	//as such. this method will return a List of type String array.
    }
  }

  /** Internal method to search by certain fields */
  private List<Customer> search(PreparedStatement stmt, Connection conn) throws Exception {
    List<Customer> conceptualcar = new LinkedList<Customer>();
    try {
      ResultSet rs = stmt.executeQuery();
      while (rs.next()) {
        conceptualcar.add(readRecord(rs, conn));
      }
    }finally{
      stmt.close();
    }
    return conceptualcar;
  }

  public Customer getByName(String fName, String lName, String pNumber) throws Data Exception{
	//establish connection and prepared statement run it through the search method, return a list of customers matching the Name and phone number
  }
  
  public Customer getByCustomerNumber(String custNum) throws Data Exception{
	//establish connection and prepared statement run it through the search method, return a Customer matching the customer number
  }


}
